<script lang="ts" setup>
import { reactive } from 'vue'
import { User, Lock, Message, Position, ChatRound,Star,Sugar } from '@element-plus/icons-vue'

const formInline = reactive({
    region1: '',
    region2: '',
})

const onSubmit = () => {
    console.log('submit!')
}
import { ref } from 'vue'

const currentPage = ref(1)
const pageSize = ref(4)
const username = ref('')
const mpostname = ref('')
const totalpage = ref()
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const dialogname = ref()
const displaydel = ref(false)
const displayjia = ref(false)



const handleSizeChange = (size) => {
    pageSize.value = size
    rpostData()
}
const handleCurrentChange = (current) => {
    currentPage.value = current
    rpostData()
}
import {
    Delete,
    Edit,
    WarningFilled
} from '@element-plus/icons-vue'

import { rpostlistServer } from '@/api/rpost.js';
import { ElMessage, FormRules } from 'element-plus';


const tableData = ref()

const reg = /^[\u4e00-\u9fa5]+$/

const timeForm = "YYYY-MM-DD HH:mm:ss"

interface RuleForm {
    content: String,
    rid: Number,
    time: Date,
    clicks: Number,
    aid: Number,
    mid: Number,
    isjing: String
}


const addData = ref<RuleForm>({
    rid: null,
    content: '',
    time: null,
    clicks: null,
    aid: null,
    mid: null,
    isjing: ''

})



const rpostData = async () => {
    let params = {
        countpage: currentPage.value,
        pagenumber: pageSize.value,
        username: username.value ? username.value : null,
        mpostname: mpostname.value ? mpostname.value : null
    }

    let result = await rpostlistServer(params)

    tableData.value = result.data.items
    totalpage.value = result.data.total
}

rpostData();

const clearadddata = () => {
    addData.value = {
        rid: null,
        content: '',
        time: null,
        clicks: null,
        aid: null,
        mid: null,
        isjing: ''
    }
}

import {  jiajingServer, delrpostServer } from '@/api/rpost.js';
import moment, { Moment } from 'moment';

const jiajing = async () => {
    displayjia.value = false;
    let result = await jiajingServer(addData.value.rid) as any;
    ElMessage.success(result.message ? result.message : '操作成功')
    clearadddata()
    rpostData()
}

const delrpost = async () => {
    displaydel.value = false
    let result = await delrpostServer(addData.value.rid) as any;
    ElMessage.success(result.message ? result.message : '删除成功')
    clearadddata()
    rpostData()
}

const rules = reactive<FormRules<RuleForm>>({
    


})


const showjiaDiglog = (row) => {
    displayjia.value = true
    dialogname.value = '提示'
    addData.value.rid = row.rid

}

const showdelDiglog = (row) => {
    displaydel.value = true
    dialogname.value = '提示'
    addData.value.rid = row.rid

}

const pFormat = (row) => {
    const stime = row.time
    return moment(stime).format('YYYY-MM-DD HH:mm')
}




</script>


<template>

    <el-card style="max-width: 100%">
        <div class="card-header">
            <span>回复贴管理</span>
        </div>
        <div style="margin-top: 20px;">
            <hr>
        </div>
        <el-form :inline="true" class="form-inline">
            <el-form-item label="发帖人" prop="username">
                <el-input placeholder="请输入待查找发帖人" v-model="username"></el-input>
            </el-form-item>
            <el-form-item label="所属主贴" prop="mpostname">
                <el-input placeholder="请输入待查找主贴名" v-model="mpostname"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="rpostData()">搜索</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="default"
                    @click="username = '',  mpostname = '', rpostData()">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" style="width: 100%" table-layout='auto'>
            <el-table-column prop="rid" label="id" />
            <el-table-column prop="username" label="作者" />
            <el-table-column prop="content" label="内容" />
            <el-table-column prop="mpostname" label="所属主贴" />
            <el-table-column prop="time" label="发帖时间" :formatter="pFormat" />
            <el-table-column prop="clicks" label="点击次数" />
            <el-table-column prop="isjing" label="是否加精" />
            <el-table-column label="操作" width="180">
                <template #default="{ row }">
                    <el-button type="primary" :icon="Star" circle @click="showjiaDiglog(row)" />
                    <el-button type="danger" :icon="Delete" circle @click="showdelDiglog(row)" />
                </template>
            </el-table-column>
        </el-table>

        <el-dialog v-model="displayjia" :title=dialogname width="500">
            <el-icon color="red">
                <Sugar />
            </el-icon>
            <span style="text-align: center;display:block;font-size: 17px;">是否确认加精该回复帖</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="jiajing()">否</el-button>
                    <el-button type="primary" @click="jiajing()">是</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog v-model="displaydel" :title=dialogname width="500">
            <el-icon color="red">
                <WarningFilled />
            </el-icon>
            <span style="text-align: center;display:block;font-size: 17px;">是否确认删除该回复贴</span>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="displaydel = false, clearadddata()">否</el-button>
                    <el-button type="primary" @click="delrpost()">是</el-button>
                </div>
            </template>
        </el-dialog>



        <!--layout控制组件展示顺序-->
        <el-pagination class="el-p" v-model:current-page="currentPage" v-model:page-size="pageSize"
            :page-sizes="[4, 8, 12, 16]" :small="small" :disabled="disabled" :background="background"
            layout="jumper, prev, pager, next, sizes, total" :total="totalpage" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
    </el-card>
</template>

<style scoped>
.el-p {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
}

.form-inline .el-input {
    --el-input-width: 220px;
}

.form-inline .el-select {
    --el-select-width: 220px;
}

.form-inline .el-date-picker {
    --el-select-width: 220px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    margin-inline-end: 20px;
}

.demo-datetime-picker {
    display: flex;
    width: 100%;
    padding: 0;
    flex-wrap: wrap;
}
</style>